package com.example.combwx.mvp.view.fragment;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.example.combwx.R;
import com.example.combwx.db.UserUtility;
import com.example.combwx.mvp.base.model.BaseEntity;
import com.example.combwx.mvp.base.view.BaseFragment;
import com.example.combwx.mvp.contract.LREContract;
import com.example.combwx.mvp.di.DaggerLREComponent;
import com.example.combwx.mvp.di.LREModules;
import com.example.combwx.mvp.model.HttpCode;
import com.example.combwx.mvp.model.entity.FollowEntity;
import com.example.combwx.mvp.model.entity.NewsEntity;
import com.example.combwx.mvp.model.entity.RecommendEntity;
import com.example.combwx.mvp.model.entity.UserEntity;
import com.example.combwx.mvp.presenter.LREPresenter;
import com.example.combwx.mvp.view.news.NewsValuesActivity;
import com.example.combwx.mvp.view.people.adapter.RecommendAdapter;
import com.example.combwx.utils.LogUtils;
import com.example.combwx.utils.ToasetUtils;
import com.scwang.smart.refresh.footer.ClassicsFooter;
import com.scwang.smart.refresh.header.ClassicsHeader;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class RecommendFragment extends BaseFragment<LREPresenter> implements
        LREContract.ILREViwe, OnItemChildClickListener
        , OnRefreshLoadMoreListener {
    private SmartRefreshLayout recommendSmart;
    private RecyclerView recommendRv;
    private LinearLayoutManager manager;
    private RecommendAdapter adapter;

    @Override
    public void onItemChildClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
        List<RecommendEntity.Values> list = adapter.getData();
        LogUtils.show("当前点击:" + list.get(position).news_title);
        RecommendEntity.Values values = (RecommendEntity.Values) adapter.getItem(position);
        String url = values.news_url;
        long id = values.news_id;
        Bundle bundle = new Bundle();
        bundle.putLong("news_id",id);
        bundle.putString("news_url",url);
        startPage(bundle, NewsValuesActivity.class);
    }
//
//    @Override
//    public View addStatusView() {
//        return f(R.id.peop_linn);
//    }
//
//    @Override
//    public int addColor() {
//        return Color.WHITE;
//    }

    @Override
    public int bindLayout() {
        return R.layout.fragment_recommend;
    }

    @Override
    public void initView() {

        recommendSmart = (SmartRefreshLayout) f(R.id.recommend_smart);
        recommendRv = (RecyclerView) f(R.id.recommend_rv);
        manager = new LinearLayoutManager(getContext());
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        recommendRv.setLayoutManager(manager);
        adapter = new RecommendAdapter();
        adapter.setOnItemChildClickListener(this);
        recommendRv.setAdapter(adapter);
        recommendSmart.setRefreshHeader(new ClassicsHeader(getContext()));
        recommendSmart.setRefreshFooter(new ClassicsFooter(getContext()));

    }

    @Override
    public void initData() {
        Map<String, Object> map = new HashMap<>();
        map.put("code", HttpCode.RECOMMENDNEWSCODE);
        p.requestAll(map);
    }

    @Override
    public void inject() {
        DaggerLREComponent.builder()
                .lREModules(new LREModules(this))
                .build().injectRecommendFragment(this);
    }

    @Override
    public void refreshAll(BaseEntity entity) {
        if (entity instanceof RecommendEntity) {
            RecommendEntity recommendEntity = (RecommendEntity) entity;
            adapter.setNewInstance(recommendEntity.getValues());
        }

    }

    @Override
    public void refreshRecyckerView(BaseEntity entity) {
        RecommendEntity recommendEntity = (RecommendEntity) entity;
        if (recommendEntity.getValues() == null) {
            ToasetUtils.showMsg("刷新失败");
            return;
        }

        adapter.setNewInstance(recommendEntity.getValues());
    }

    @Override
    public void loadMoreRecyclerView(BaseEntity entity) {
        RecommendEntity recommendEntity = (RecommendEntity) entity;
        if (recommendEntity.getValues() == null) {
            ToasetUtils.showMsg("最后一页");
            return;
        }
        adapter.addData(recommendEntity.getValues());
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        refreshLayout.finishLoadMore();
        UserEntity entity = null;
        try {
            entity = UserUtility.getInstance().getDaoSession().queryBuilder(UserEntity.class).list().get(0);
            Map<String, Object> newsMap = new HashMap<>();
            newsMap.put("code", HttpCode.RECOMMENDNEWSCODE);
            newsMap.put("user_id", 1);
            p.requestLoad(newsMap);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        refreshLayout.finishRefresh();
        UserEntity entity = null;
        try {
            entity = UserUtility.getInstance().getDaoSession().queryBuilder(UserEntity.class).list().get(0);
            Map<String, Object> newsMap = new HashMap<>();
            newsMap.put("code", HttpCode.RECOMMENDNEWSCODE);
            newsMap.put("user_id", 1);
            p.requestRefresh(newsMap);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
